package com.alphathink.qrcodecard.Activity;

import android.app.Activity;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Button;
import android.widget.LinearLayout.LayoutParams;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.alphathink.qrcodecard.model.QRCodeCardModel;
import com.alphathink.qrcodecard.model.QRCodeCardNetModel;
import com.alphathink.xpg.qrcodecard.R;
import com.google.gson.Gson;
import com.zxing.encoding.EncodingHandler;

public class ScanQRResult extends Activity {

	private static final String TAG = "ScanQRResult";
	private TextView mName, mPhoneNum, mQQNum, mWeiBo;
	private Button mOKBtn, mCancleBtn;
	private ImageView mImagetView;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_scan_qrresult);

		iniView();
		initData();
	}

	private void iniView() {
		mImagetView = (ImageView) findViewById(R.id.scan_qrresult_IV_QR);
		mName = (TextView) findViewById(R.id.main_NameTV);
		mPhoneNum = (TextView) findViewById(R.id.main_PhoneTV);
		mQQNum = (TextView) findViewById(R.id.main_qqTV);
		mWeiBo = (TextView) findViewById(R.id.main_SinaTV);

		mOKBtn = (Button) findViewById(R.id.main_OKBtn);
		mCancleBtn = (Button) findViewById(R.id.main_CancleBtn);
	}

	private void initData() {
		// 获取扫描之后的信息
		String mStr = getIntent().getExtras().getString("data");

		QRCodeCardNetModel cardNetModel = null;
		try {
			Gson gson = new Gson();
			cardNetModel = gson.fromJson(mStr, QRCodeCardNetModel.class);
		} catch (Exception e) {
			e.printStackTrace();
		}
		if (cardNetModel == null) {
			Toast.makeText(getApplication(), "出错了，请重新扫描!", Toast.LENGTH_SHORT)
					.show();
			ScanQRResult.this.finish();
		} else if ((QRCodeCardModel) cardNetModel.getData().get(0) == null) {
			Toast.makeText(getApplication(), "空白的名片！", Toast.LENGTH_SHORT)
					.show();
			ScanQRResult.this.finish();
		} else {
			QRCodeCardModel cardModel = (QRCodeCardModel) cardNetModel
					.getData().get(0);
			System.out.println(TAG + "cardModel---->" + cardModel);

			Log.d(TAG, mStr);
			final Bitmap mBitMap;
			try {
				mBitMap = EncodingHandler.createQRCode(mStr, 300);
				mImagetView.setImageBitmap(mBitMap);
				Log.d(TAG, "----扫描别人的二维码之后生成成功---->");
			} catch (Exception e) {
				e.printStackTrace();
			}

			mName.setText(cardModel.getUserName());
			mPhoneNum.setText(cardModel.getUserPhoneNum());
			mQQNum.setText(cardModel.getUserQQNum());
			mWeiBo.setText(cardModel.getUserWeiBo());
		}

	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.scan_qrresult, menu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		// Handle action bar item clicks here. The action bar will
		// automatically handle clicks on the Home/Up button, so long
		// as you specify a parent activity in AndroidManifest.xml.
		int id = item.getItemId();
		if (id == R.id.action_settings) {
			return true;
		}
		return super.onOptionsItemSelected(item);
	}
}
